import './css/index.scss'
import DialogDom from "./DialogDom";

type _dialogs = {
    params: unknown,
    callback: fun,
    dom: DialogDom,
}

class Dialog {

    /**
     * 弹窗层级
     * @private
     */
    private zIndex: number = 0;

    /**
     * 弹窗队列
     * @private
     */
    private dialogs: { [k: string]: _dialogs } = {};

    /**
     * 获取参数
     * @param name
     */
    getParams(name: string) {
        const obj = this.dialogs[name];
        return obj.params;
    }

    /**
     * 显示
     * @param opt
     */
    show(opt: Opt) {
        const options: Options = {
            height: opt.height || '300px',
            width: opt.width || '500px',
            title: opt.title || '',
            src: opt.src || '',
            max: false,
            zIndex: ++this.zIndex,
            callback: (data) => {
                opt.callback && opt.callback(data);
            },
            closeHandler: (data) => {
                opt.closeHandler && opt.closeHandler(data);
                delete this.dialogs[opt.name]
            },
            helpHandler: () => {
                opt.helpHandler && opt.helpHandler();
            },
        }
        const dom = new DialogDom(options);
        this.dialogs[opt.name] = {
            callback: (data: unknown) => {
                opt.callback && opt.callback(data);
            },
            params: opt.params,
            dom,
        };
    }

    close(name: string, data?: unknown) {
        typeof data != 'undefined' && this.callback(name, data)
        this.dialogs[name].dom.close({type: '_btn'})
    }

    callback(name: string, data: unknown) {
        this.dialogs[name].callback(data)
    }
}

// @ts-ignore
window.Dialog = Dialog;
